package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.form.InviteCodeForm;
import com.yuantu.judiciary.model.vo.InviteCodeDivideVO;
import com.yuantu.judiciary.model.vo.InviteRecordDivideVO;
import com.yuantu.judiciary.service.IInviteCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 邀请码模块
 *
 * @author syw
 * @since 2023-08-17
 **/
@RestController
@RequestMapping("/invite-code")
@Api(tags = "邀请码模块")
@RequiredArgsConstructor
public class InviteCodeController extends BaseController {

    private final IInviteCodeService inviteCodeService;

    @ApiOperation(value = "获取随机码")
    @GetMapping("/get/random")
    public Response<String> getRandom() {
        return ResponseFactory.okResponse(inviteCodeService.getRandom());
    }

    @ApiOperation(value = "绑定推荐码")
    @PostMapping("/bind-trial")
    public Response<Boolean> bindTrial(@RequestBody @Validated InviteCodeForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(inviteCodeService.bindTrial(accountId, form));
    }

    @ApiOperation(value = "绑定邀请码")
    @PostMapping("/bind-invite")
    public Response<Boolean> bindInvite(@RequestBody @Validated InviteCodeForm form) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(inviteCodeService.bindInvite(accountId, form));
    }

    @ApiOperation(value = "所有已有无邀请码的账号新建一个邀请码")
    @GetMapping("/update-invite")
    public Response<Boolean> updateInvite() {
        inviteCodeService.addInviteCodeToAllAccountWithoutInviteCode();
        return ResponseFactory.okResponse(Boolean.TRUE);
    }

    @ApiOperation(value = "查询分配使用情况")
    @GetMapping("/divide/info")
    public Response<List<InviteCodeDivideVO>> queryDivideInfos(@RequestParam(required = false) String codeInfo,
                                                               @RequestParam Long accountId) {
        return ResponseFactory.okResponse(inviteCodeService.queryDivideInfos(codeInfo, accountId));
    }


    @ApiOperation(value = "查询分配使用情况详情")
    @GetMapping("/divide/info/detail")
    public Response<List<InviteRecordDivideVO>> queryDivideInfoDetail(String codeInfo) {
        return ResponseFactory.okResponse(inviteCodeService.queryDivideInfoDetail(codeInfo));
    }

}
